# $Id: PKGBUILD 180092 2013-03-16 15:44:29Z tpowa $
# Maintainer: Tobias Powalowski <tpowa@archlinux.org>
# Maintainer : Keshav P R <(the.ridikulus.rat) (aatt) (gemmaeiil) (ddoott) (ccoomm)>
# Contributor: Alessio 'mOLOk' Bolognino <themolok@gmail.com>

_pkgver="3.0"
pkgname="gnu-efi-libs"
pkgver="${_pkgver}t"
pkgrel="2"
pkgdesc="Library for building x86_64 and i386 UEFI Applications using GNU toolchain"
url="http://sourceforge.net/projects/gnu-efi/"
license=('GPL')
arch=('i686' 'x86_64')
makedepends=('pciutils')
if [[ "${CARCH}" == "x86_64" ]]; then
	makedepends+=('lib32-glibc')
fi
conflicts=('gnu-efi')
provides=('gnu-efi')
replaces=('gnu-efi')
options=('!strip' '!makeflags')
source=("http://download.sourceforge.net/gnu-efi/gnu-efi_${pkgver}.orig.tar.gz"
        'disable-ms_abi-flag.patch')         
_build_gnu-efi-libs-x86_64() {	
	cp -r "${srcdir}/gnu-efi-${_pkgver}" "${srcdir}/gnu-efi-${_pkgver}-x86_64"
	cd "${srcdir}/gnu-efi-${_pkgver}-x86_64/"
	ARCH="x86_64" make
	ARCH="x86_64" make -C apps all	
}

_build_gnu-efi-libs-i386() {
	cp -r "${srcdir}/gnu-efi-${_pkgver}" "${srcdir}/gnu-efi-${_pkgver}-i386"
	cd "${srcdir}/gnu-efi-${_pkgver}-i386/"	
	ARCH="ia32" make
	ARCH="ia32" make -C apps all	
}

build() {
        cd ${srcdir}/gnu-efi-${_pkgver}
        # fix broken ms_abi flags, causing weird issues with bootloaders
        patch -Np1 -i ../disable-ms_abi-flag.patch
	## Fix Makefiles to enable compile for both UEFI arch
	sed 's|INSTALLROOT=/usr/local|INSTALLROOT ?= /usr/lib|g' -i Make.defaults
	sed 's|LIBDIR=lib|LIBDIR ?= lib|g' -i Make.defaults
	sed 's|ARCH	   :=|ARCH	   ?=|g' -i Make.defaults
	sed 's|-fno-strict-aliasing|-fno-strict-aliasing -fno-stack-protector|g' -i Make.defaults
	if [[ "${CARCH}" == "x86_64" ]]; then
		_build_gnu-efi-libs-x86_64
	fi
	_build_gnu-efi-libs-i386	
}

_package_gnu-efi-libs-x86_64() {
	cd "${srcdir}/gnu-efi-${_pkgver}-x86_64/"	
	make ARCH="x86_64" PREFIX="" INSTALLROOT="${pkgdir}/usr/" LIBDIR="lib" install
	install -d "${pkgdir}/usr/share/gnu-efi/x86_64/"
	install -D -m0644 "${srcdir}/gnu-efi-${_pkgver}-x86_64/apps"/*.efi "${pkgdir}/usr/share/gnu-efi/x86_64/"
}

_package_gnu-efi-libs-i386() {	
	cd "${srcdir}/gnu-efi-${_pkgver}-i386/"
	make ARCH="ia32" PREFIX="" INSTALLROOT="${pkgdir}/usr/" LIBDIR="${_LIBDIR32}" install
	install -d "${pkgdir}/usr/share/gnu-efi/i386/"
	install -D -m0644 "${srcdir}/gnu-efi-${_pkgver}-i386/apps"/*.efi "${pkgdir}/usr/share/gnu-efi/i386/"	
}

package() {
	if [[ "${CARCH}" == "x86_64" ]]; then
		_package_gnu-efi-libs-x86_64
		
		_LIBDIR32="lib32"
		_package_gnu-efi-libs-i386
	else
		_LIBDIR32="lib"
		_package_gnu-efi-libs-i386
	fi	
}
md5sums=('95916208cf543699799230ac1ea14272'
         '783755e5f691f4c5c629f94d759b727b')
